﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class frmQMK : Form
    {
        public frmQMK()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            string str1 = null;
            string str2 = null;
            if (txtUser == null || txtHint == null)
            {
                MessageBox.Show("Nhập tên sử dụng và từ gợi ý!", "Report", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                string kq;
                kq = ForgetPass(str1, str2).ToString();
                if (kq == "0")
                {
                    MessageBox.Show("Nhập sai username hoặc hint!", "Report", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    this.Hide();
                    MessageBox.Show( "Mật khẩu của bạn là " + kq, "Report", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        public string ForgetPass(String str1, String str2)
        {
            RibbonForm1 rb = new RibbonForm1();
            SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=SM;Integrated Security=True");
            SqlCommand cmd;
            string kq;
            cmd = new SqlCommand();
            cmd.CommandText = "Forgetpass";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = connection;
            connection.Open();
            cmd.Parameters.AddWithValue("@Username", txtUser.Text);
            cmd.Parameters.AddWithValue("@Hint", txtHint.Text);
            SqlParameter returnvalue = new SqlParameter("@pass", "");
            returnvalue.Direction = ParameterDirection.ReturnValue;
            cmd.Parameters.Add(returnvalue);
            cmd.ExecuteNonQuery();
            kq = (returnvalue.Value).ToString();
            connection.Close();
            return kq;
            cmd.ExecuteNonQuery();
            connection.Close();
        }
    }
}
